package cn.piesat.springsecurity.base.entity;

import cn.piesat.springsecurity.base.BaseCodeEnum;
import cn.piesat.springsecurity.base.CommonCodeEnum;
import com.alibaba.fastjson.JSON;

import java.io.Serializable;

public class BaseResponse<T> implements Serializable {
    private static final long serialVersionUID = -1L;
    private String code;
    private String message;
    private T data;

    public BaseResponse() {
    }

    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean OK() {
        return CommonCodeEnum.SUCCESS.getCode().equals(this.code);
    }

    public static final <E> BaseResponse<E> build(String code, String message, E data) {
        BaseResponse<E> response = new BaseResponse();
        response.code = code;
        response.message = message;
        response.data = data;
        return response;
    }

    public static final <E> BaseResponse<E> build(BaseCodeEnum codeEnum, E data) {
        BaseResponse<E> response = new BaseResponse();
        response.code = codeEnum.getCode();
        response.message = codeEnum.getMessage();
        response.data = data;
        return response;
    }

    public static final <E> BaseResponse<E> fail(String code, String message) {
        return build(code, message, null);
    }

    public static final <E> BaseResponse<E> fail(String code, String message, E data) {
        return build(code, message, data);
    }

    public static final <E> BaseResponse<E> fail(BaseCodeEnum codeEnum) {
        return build(codeEnum, null);
    }

    public static final <E> BaseResponse<E> fail(BaseCodeEnum codeEnum, E data) {
        return build(codeEnum, data);
    }

    public static final <E> BaseResponse<E> success(E data) {
        return build(CommonCodeEnum.SUCCESS, data);
    }

    public static final <E> BaseResponse<E> success() {
        return build(CommonCodeEnum.SUCCESS, null);
    }

    public String toJsonString() {
        return JSON.toJSONString(this);
    }
}
